/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Berekenen.Calc;
import Berekenen.Check;
import Berekenen.DetCheckResult;
import Berekenen.MaakMatrix;
import Components.FileType;
import Converter.Ansys;
import Delta.Laminaat;
import Jama.Matrix;
import javax.swing.ButtonGroup;
import laminaatapplicatie.Global;

/**
 *
 * @author Memet
 */
public class exportToAnsysForm extends javax.swing.JFrame {

    Laminaat lam;
    /**
     * Creates new form exportToAnsysForm
     */
    public exportToAnsysForm() {
        initComponents();
        
        ButtonGroup group = new ButtonGroup();
        group.add(rbtnBulk);
        group.add(rbtnPreIntegrated);
        group.add(rbtnTotal);
    }

    public void setLam(Laminaat lam)
    {
        this.lam = lam;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtCoordsystem = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        rbtnPreIntegrated = new javax.swing.JRadioButton();
        rbtnBulk = new javax.swing.JRadioButton();
        rbtnTotal = new javax.swing.JRadioButton();
        btnExport = new javax.swing.JButton();
        cbSecoffset = new javax.swing.JComboBox();
        lblSecOffset = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        txtCoordsystem.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCoordsystem.setText("12");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Method"));

        rbtnPreIntegrated.setSelected(true);
        rbtnPreIntegrated.setText("Pre Integrated");
        rbtnPreIntegrated.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnPreIntegratedActionPerformed(evt);
            }
        });

        rbtnBulk.setText("Bulk Laminate");
        rbtnBulk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnBulkActionPerformed(evt);
            }
        });

        rbtnTotal.setText("Total Laminate");
        rbtnTotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnTotalActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbtnPreIntegrated)
                    .addComponent(rbtnTotal)
                    .addComponent(rbtnBulk))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(rbtnPreIntegrated)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnTotal)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnBulk))
        );

        btnExport.setText("Export");
        btnExport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportActionPerformed(evt);
            }
        });

        cbSecoffset.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TOP", "MID", "BOT" }));
        cbSecoffset.setSelectedIndex(1);
        cbSecoffset.setEnabled(false);

        lblSecOffset.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblSecOffset.setText("Secoffset");
        lblSecOffset.setEnabled(false);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("CoordSystem");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtCoordsystem)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblSecOffset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbSecoffset, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnExport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtCoordsystem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblSecOffset)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbSecoffset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnExport)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rbtnPreIntegratedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnPreIntegratedActionPerformed
        rbtnTotalActionPerformed(evt);
    }//GEN-LAST:event_rbtnPreIntegratedActionPerformed

    private void rbtnBulkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnBulkActionPerformed
        rbtnTotalActionPerformed(evt);
    }//GEN-LAST:event_rbtnBulkActionPerformed

    private void rbtnTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnTotalActionPerformed
        if (rbtnTotal.isSelected()) {
            cbSecoffset.setEnabled(true);
            lblSecOffset.setEnabled(true);
        } else {
            cbSecoffset.setEnabled(false);
            lblSecOffset.setEnabled(false);
        }
    }//GEN-LAST:event_rbtnTotalActionPerformed

    private void btnExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportActionPerformed
        int csystem = Integer.parseInt(txtCoordsystem.getText());

        if (rbtnBulk.isSelected()) {
            //bulk
            adplView frm = new adplView();
            //Matrix ek = MaakMatrix.EKMat(MaakMatrix.NMmat(MaakMatrix.Nt(lam.getLam(), Global.data), MaakMatrix.Mt(lam.getLam(), Global.data)), MaakMatrix.A(lam.getLam(), Global.data), MaakMatrix.B(lam.getLam(), Global.data), MaakMatrix.D(lam.getLam(), Global.data));
            
            double a1 = lam.getA1();
            //double a1 = Calc.A1(lam.getLam(), Global.data);
            double a2 = lam.getA2();
            //double a2 = Calc.A2(lam.getLam(), Global.data);
            double a3 = lam.getA3();
            //double a3 = Calc.A3(lam.getLam(), Global.data);

//            double e1 = Calc.E1(lam.getLam(), Global.data);
            double e1 = lam.getE1();
//            double e2 = Calc.E2(lam.getLam(), Global.data);
            double e2 = lam.getE2();
//            double e3 = Calc.E3(lam.getLam(), Global.data);
            double e3 = lam.getE3();
//            double g12 = Calc.G12(lam.getLam(), Global.data);
            double g12 = lam.getG12();
//            double g23 = Calc.G23(lam.getLam(), Global.data);
            double g23 = lam.getG23();
//            double g31 = Calc.G31(lam.getLam(), Global.data);
            double g31 = lam.getG31();
//            double v12 = Calc.Mu12(lam.getLam(), Global.data);
            double v12 = lam.getMu12();
//            double v23 = Calc.Nu23(lam.getLam(), Global.data);
            double v23 = lam.getMu23();
//            double v31 = Calc.Nu31(lam.getLam(), Global.data);
            double v31 = lam.getMu31();
            DetCheckResult res = Check.positiveDef(e1, e2, e3, v12, v23, v31, g12, g23, g31);
            String text = Ansys.startCode(csystem) + Ansys.ToAnsysADPL(e1, e2, e3, g12, g23, g31, v12, v23, v31, lam.getRho(), lam.getName(), a1, a2, a3);
            frm.setText(text, res.toString());
            frm.setType(FileType.MP);
            frm.setVisible(true);
        } else if (rbtnPreIntegrated.isSelected()) {
            //pre integrated
            adplView frm = new adplView();
//            String text = Ansys.startCode(csystem) + Ansys.ToAnsysADPL(MaakMatrix.A(lam.getLam(), Global.data), MaakMatrix.B(lam.getLam(), Global.data), MaakMatrix.D(lam.getLam(), Global.data), MaakMatrix.Nt(lam.getLam(), Global.data), MaakMatrix.Mt(lam.getLam(), Global.data), Calc.Rho(lam.getLam(), Global.data));
            //TODO: (met Bart) check of N (nieuwe code) == Nt (oude code) en zelfde voor M == Mt
            String text = Ansys.startCode(csystem) + Ansys.ToAnsysADPL(lam.getA(),lam.getB(),lam.getD(), lam.getNt(), lam.getMt(), lam.getRho());
            frm.setText(text);
            frm.setType(FileType.MP);
            frm.setVisible(true);
        } else {
            //total
            adplView frm = new adplView();
//            String text = Ansys.startCode(csystem) + Ansys.ToAnsysADPL(lam, String.valueOf(cbSecoffset.getSelectedItem()));
            String text = Ansys.startCode(csystem) + Ansys.ToAnsysADPL(lam.getE1(), lam.getE2(), lam.getE3(), lam.getG12(), lam.getG23(), lam.getG31(), lam.getMu12(), lam.getMu23(), lam.getMu31(), lam.getRho(), lam.getName(), lam.getA1(), lam.getA2(), lam.getA3());
            frm.setText(text);
            frm.setType(FileType.MP);
            frm.setVisible(true);
        }
        this.dispose();
    }//GEN-LAST:event_btnExportActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(exportToAnsysForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(exportToAnsysForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(exportToAnsysForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(exportToAnsysForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new exportToAnsysForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExport;
    private javax.swing.JComboBox cbSecoffset;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblSecOffset;
    private javax.swing.JRadioButton rbtnBulk;
    private javax.swing.JRadioButton rbtnPreIntegrated;
    private javax.swing.JRadioButton rbtnTotal;
    private javax.swing.JTextField txtCoordsystem;
    // End of variables declaration//GEN-END:variables
}
